package com.example.enterprise.controller;

import com.example.enterprise.entity.*;
import com.example.enterprise.service.PictureService;
import com.example.enterprise.service.SeLogService;
import com.example.enterprise.util.CommunityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

import static com.example.enterprise.util.CommunityUtil.getFiledName;


@Controller
public class SeLogController {

    @Autowired
    private SeLogService seLogService;

    @Autowired
    private PictureService pictureService;


    //展示企业自己的安全日志,增加隐患表,draft=0,type=1
    @RequestMapping(path = "/security-log", method = {RequestMethod.GET, RequestMethod.POST})
    public String getSeLogPage(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start, @DateTimeFormat(pattern = "yyyy-MM-dd") Date end, Model model, Page page) {
        // 方法调用钱,SpringMVC会自动实例化Model和Page,并将Page注入Model.
        // 所以,在thymeleaf中可以直接访问Page对象中的数据.


        //得到enterprise
        int enterpriseId = 1;


        //返回日志列表
        List<SeLogWithBLOBs> list;


        if (start == null && end == null) {
            list = seLogService.findSeLogs(enterpriseId, page.getOffset(), page.getLimit());
            page.setRows(seLogService.findSeLogRows(enterpriseId));
            page.setPath("/security-log");
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String starTime = sdf.format(start);
            String endTime = sdf.format(end);


            list = seLogService.findSeLogsByDate(enterpriseId, page.getOffset(), page.getLimit(), start, end);
            page.setRows(seLogService.findSeLogRowsByDate(enterpriseId, start, end));
            page.setPath("/security-log?start=" + starTime + "&end=" + endTime);
        }


        List<Map<String, Object>> discussPosts = new ArrayList<>();
        if (list != null) {
            for (SeLogWithBLOBs post : list) {
                Map<String, Object> map = new HashMap<>();
                map.put("post", post);
                discussPosts.add(map);
            }
        }
        model.addAttribute("discussPosts", discussPosts);
        model.addAttribute("uvStartDate", start);
        model.addAttribute("uvEndDate", end);
        return "site/security-log";
    }


    //添加企业自己的安全日志表,将草稿信息回显
    @RequestMapping(path = "/add-selog", method = RequestMethod.GET)
    public String getAddSeLogPage(Model model) {

        int enterpriseId = 1;


        //查询是否有草稿
        //传入草稿数据
        if (seLogService.haveSeLogDraft(enterpriseId)) {

            List<SeLogWithBLOBs> draftLogs = seLogService.findSeLogDraft(enterpriseId);
            SeLogWithBLOBs draft=draftLogs.get(0);
            //反射得到属性和值
            Map<String, Object> map = getFiledName(draft);

            for (Map.Entry<String, Object> entry : map.entrySet()) {
//              System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
                model.addAttribute(entry.getKey(), entry.getValue());
            }

            //寻找草稿图片
            List<Picture> pictures=pictureService.getPicBySeLogId(draft.getId());
            model.addAttribute("pictures",pictures);

        }


        return "site/add-selog";
    }

    //提交草稿，提交安全日志
    @RequestMapping(path = "/add-selog", method = RequestMethod.POST)
    @ResponseBody
    public String addSeLogPage(Model model, SeLogWithBLOBs seLogWithBLOBs) {

        //先得到enterprise,一个enterprise只有一个草稿
        int enterpriseId = 1;
        seLogWithBLOBs.setEnterpriseId(enterpriseId);

        Map<String, Object> message = new HashMap<>();


        //是否草稿,信息存入message，再跳转到企业页面
        if (seLogWithBLOBs.getDraft() == 1) {

            //查找该企业是否有草稿了,是的话将原来草稿作废
            if(seLogService.haveSeLogDraft(enterpriseId)){
                int flag=seLogService.changeSeLogDraft(enterpriseId);
            }


            //添加新草稿
            int flag = seLogService.insertSeLog(seLogWithBLOBs);
            if (flag == 1) {


                message.put("addSeLogInfo", "日志草稿保存成功");
            } else {


                message.put("addSeLogInfo", "日志草稿保存失败");
            }


//            return "forward:/security-log";

            return CommunityUtil.getJSONString(0,"msg",message);

        }
        //提交安全日志
        else {

            //查找该企业是否有草稿了,是的话将原来草稿状态改为0
            if(seLogService.haveSeLogDraft(enterpriseId)){
                int flag=seLogService.changeSeLogDraft(enterpriseId);
            }
            else{
                int flag = seLogService.insertSeLog(seLogWithBLOBs);
            }

            message.put("addSeLogInfo", "日志提交成功");


//            return "forward:/security-log";
            return CommunityUtil.getJSONString(0,"msg",message);


        }


    }


}
